package com.sl.biz.modular.process.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.process.controller.request.ProcessRunHistoryQueryProcessListRequest;
import com.sl.biz.modular.process.controller.request.ProcessRunHistoryQueryProcessStepDataListRequest;
import com.sl.biz.modular.process.controller.request.ProcessRunHistoryQueryProcessStepListRequest;
import com.sl.biz.modular.process.controller.response.ProcessRunHistoryListVO;
import com.sl.biz.modular.process.controller.response.ProcessRunHistoryStepListVO;
import com.sl.biz.modular.process.handler.ProcessRunHistoryHandler;
import com.sl.biz.modular.process.service.ProcessRunHistoryBizService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ProcessRunHistoryBizServiceImpl implements ProcessRunHistoryBizService {


    private final List<ProcessRunHistoryHandler> processRunHistoryHandlers;


    @Override
    public List<ProcessRunHistoryListVO> processList(ProcessRunHistoryQueryProcessListRequest processListRequest) {

        List<ProcessRunHistoryListVO> result = Lists.newArrayList();
        for (ProcessRunHistoryHandler processRunHistoryHandler : processRunHistoryHandlers) {
            List<ProcessRunHistoryListVO> processRunHistoryListVOS = processRunHistoryHandler.processList(processListRequest);
            if (CollUtil.isNotEmpty(processRunHistoryListVOS)) {
                result.addAll(processRunHistoryListVOS);
            }
        }
        return result;
    }

    @Override
    public List<String> runDateList() {

        List<String> result = Lists.newArrayList();
        for (ProcessRunHistoryHandler processRunHistoryHandler : processRunHistoryHandlers) {
            List<String> list = processRunHistoryHandler.runDateList();
            if (CollUtil.isNotEmpty(list)) {
                result.addAll(list);
            }
        }
        return result.stream().distinct().toList();
    }

    @Override
    public List<ProcessRunHistoryStepListVO> stepList(ProcessRunHistoryQueryProcessStepListRequest processListRequest) {
        String type = processListRequest.getType();
        List<ProcessRunHistoryStepListVO> result = Lists.newArrayList();
        for (ProcessRunHistoryHandler processRunHistoryHandler : processRunHistoryHandlers) {
            if (!StrUtil.equals(processRunHistoryHandler.type(), type)) {
                continue;
            }
            List<ProcessRunHistoryStepListVO> list = processRunHistoryHandler.stepList(processListRequest);
            if (CollUtil.isNotEmpty(list)) {
                result.addAll(list);
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> stepDataList(ProcessRunHistoryQueryProcessStepDataListRequest processListRequest) {
        String type = processListRequest.getType();
        Map<String, Object> map = null;
        for (ProcessRunHistoryHandler processRunHistoryHandler : processRunHistoryHandlers) {
            if (!StrUtil.equals(processRunHistoryHandler.type(), type)) {
                continue;
            }
            map = processRunHistoryHandler.stepDataList(processListRequest);
            break;
        }
        return map;
    }


}
